package com.zxy.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.zxy.common.result.Result;

import com.zxy.pojo.dto.ContractDTO;
import com.zxy.pojo.dto.CourseContractDTO;
import com.zxy.pojo.dto.PageDTO;
import com.zxy.pojo.entity.CourseContract;
import com.zxy.pojo.vo.CourseContractResponseVO;
import com.zxy.service.ContractService;
import com.zxy.util.OperLog;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

/*合同管理*/
@RestController
@RequestMapping

public class ContractController {

    @Autowired
    private ContractService contractService;
    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    //分页查询
    @OperLog(title = "合同分页查询", businessType = 0)
    @GetMapping("/contract/list")
    public CourseContractResponseVO<CourseContractDTO> listContract(PageDTO pageDTO) {
        return contractService.listContract(pageDTO);
    }

    //条件查询课程和学科信息
    @OperLog(title = "添加合同", businessType = 1)
    @GetMapping("/clues/course/listselect")  // /clues/course/listselect
    Result listSelectCourse(@RequestParam(value = "subject", required = false) String subject
    ,  @RequestParam(value = "courseId", required = false) Long courseId) {
        return Result.success(contractService.listSelectCourse(subject,courseId));
    }

    //渠道来源
    @GetMapping("/clues/activity/listselect/{channel}")
    Result listSelectActivity(@PathVariable String channel) {
        return Result.success(contractService.listSelectActivity(channel));
    }

    //添加合同
    @PostMapping("/contract")
    Result addContract(@RequestBody ContractDTO contractDTO) {
        contractService.addContract(contractDTO);
        return Result.success();
    }

    //预览合同  问题
    //TODO
    @GetMapping("/profile/upload/{year}/{month}/{day}/{fileId}.pdf")
    String upload(@PathVariable String year, @PathVariable String month, @PathVariable String day, @PathVariable String fileId) {
        return redisTemplate.opsForValue().get("filename");
    }

    //根据id回显
    @GetMapping("/contract/{id}")
    Result detail(@PathVariable Long id) {
        return Result.success(contractService.detail(id));
    }

    @GetMapping("/system/notice/list/{id}")
    Result listNotice(@PathVariable String id) {
        return Result.success();
    }

    //根据id修改数据
    @GetMapping("/contract/detail/{id}")
    Result update(@PathVariable String id) {
        CourseContract contract = contractService.getById(id);
        CourseContractDTO dto = new CourseContractDTO();
        BeanUtils.copyProperties(contract, dto);
        dto.setId(id);
        dto.setOrder(contract.getOrderAmount());
        return Result.success(dto);
    }

    //修改数据
    @PutMapping("/contract")
    Result update(@RequestBody ContractDTO cos) {
        LambdaUpdateWrapper<CourseContract> wrapper = new LambdaUpdateWrapper<>();
        if (StringUtils.isNotBlank(cos.getName())) {
            wrapper.set(CourseContract::getName, cos.getName());
        }
        if (StringUtils.isNotBlank(cos.getPhone())) {
            wrapper.set(CourseContract::getPhone, cos.getPhone());
        }
        if (StringUtils.isNotBlank(cos.getContractNo())) {
            // 查询数据库中是否有其他记录使用该contractNo
            LambdaQueryWrapper<CourseContract> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(CourseContract::getContractNo, cos.getContractNo())
                    .ne(CourseContract::getId, cos.getId()); // 排除自身记录
            long count = contractService.count(queryWrapper);
            if (count > 0) {
                return Result.error("合同编号已存在：" + cos.getContractNo());
            }
            if (StringUtils.isNotBlank(cos.getCourseId())) {
                wrapper.set(CourseContract::getCourseId, cos.getCourseId());
            }
            if (StringUtils.isNotBlank(cos.getChannel())) {
                wrapper.set(CourseContract::getChannel, cos.getChannel());
            }
            if (cos.getActivityId() != null) {
                wrapper.set(CourseContract::getActivityId, cos.getActivityId());
            }
            if (StringUtils.isNotBlank(cos.getFileName())) {
                wrapper.set(CourseContract::getFileName, cos.getFileName());
            }
            wrapper.eq(CourseContract::getId, cos.getId());
            CourseContract contract = new CourseContract();
            BeanUtils.copyProperties(cos, contract);
            contractService.update(contract, wrapper);
        }
        return Result.success();
    }
}


